# coding=utf-8
import json
from abc import ABCMeta

import tornado.web

from CommonLib import CheckJsonKeys, CheckAuth
from WrapCursor import WrapCursor
from database.models.DatabaseCommon import *
from database.models.OperationSet import OperationSet


class GetNormalStepHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor()
        try:
            ojson = json.loads(self.request.body)
            t1 = ojson['mode_id']
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            self.write(r.Error('request data error ' + str(e)))
            return
        normalID = ojson['mode_id']
        s = Session()
        try:
            osets = s.query(OperationSet).filter_by(NormalID=normalID).all()
        except Exception as e:
            jsonret = {'Code': 1, 'desc': u'查询数据库失败', 'data': ''}
            self.write(json.dumps(jsonret))
            return
        data = list()
        jsonret = {'Code': 0, 'desc': u'成功', 'data': data}

        for i in osets:
            o = dict()
            o['PointID'] = i.PointID
            o['Opt'] = i.OptTypeID
            o['Params'] = i.Params
            data.append(o)
        self.write(json.dumps(jsonret))
        return
